package com.hospital;

import java.util.ArrayList;
import java.util.List;

import net.tsz.afinal.http.AjaxParams;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

import com.google.gson.reflect.TypeToken;
import com.hospital.pony.adapter.PractitionersAdapter;
import com.hospital.pony.base.BaseActivity;
import com.hospital.pony.config.Consts;
import com.hospital.pony.db.MemberUserUtils;
import com.hospital.pony.db.ShowObservable;
import com.hospital.pony.model.ApplyModel;
import com.hospital.pony.model.DortorModel;
import com.hospital.pony.model.PersonModel;
import com.hospital.pony.model.ResponseEntry;
import com.hospital.pony.util.ToastUtil;
import com.hospital.pony.view.DialogListMsg;

public class UpdatePayActivity extends BaseActivity {

	// 标题
	private TextView mTvTitle;
	// 返回
	private ImageView mIvBack;
	private TextView mtvMsg;
	private Button mPay;
	private int choiceType = 1;
	private RadioGroup mrdChoice;
	private RadioButton mrbWeiXin = null;
	private RadioButton mrbPay = null;
	private TextView mtvName;
	DortorModel noticeModel;
	
	private int posIndex = 0;
	private DialogListMsg dialogListMsg;
	private PractitionersAdapter listaAdapter;
	private ApplyModel applyModel;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_pay_message);
		initWidget();
		initData();
	}

	@Override
	public void onClick(View v) {
		if(v.getId()==R.id.mIvBack){
			finish();
		}
		if(v.getId()==R.id.mPay){
			addMessage(true);
		}
	}

	@Override
	public void initWidget() {
		
		mrdChoice = (RadioGroup) findViewById(R.id.mrdChoice);
		mrbWeiXin = (RadioButton) findViewById(R.id.mrbWeiXin);
		mrbPay = (RadioButton) findViewById(R.id.mrbPay);
		mtvName = (TextView) findViewById(R.id.mtvName);
		mrdChoice = (RadioGroup) findViewById(R.id.mrdChoice);
		mrbWeiXin = (RadioButton) findViewById(R.id.mrbWeiXin);
		mrbPay = (RadioButton) findViewById(R.id.mrbPay);
		mPay = (Button) findViewById(R.id.mPay);
		mtvMsg = (TextView) findViewById(R.id.mtvMsg);
		mIvBack = (ImageView) findViewById(R.id.mIvBack);
		mTvTitle = (TextView) findViewById(R.id.mTvTitle);
		mTvTitle.setText("预约确认");
		mIvBack.setVisibility(View.VISIBLE);
		mIvBack.setOnClickListener(this);
		mPay.setOnClickListener(this);
	}

	@Override
	public void initData() {
		applyModel = (ApplyModel) this.getIntent().getSerializableExtra("applyModel");

		noticeModel = (DortorModel) this.getIntent().getSerializableExtra("msg");
		mtvMsg.setText(noticeModel.getDoctorNumber()+"元");
		mtvName.setText(noticeModel.getDoctorName());
		
		mrdChoice.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				if (checkedId == mrbWeiXin.getId()) {
					choiceType = 1;
				} else if (checkedId == mrbPay.getId()) {
					choiceType = 2;
				}
			}
		});
		
		

		dialogListMsg = new DialogListMsg(this);
		dialogListMsg.setTitle().setText("请选择患者");
		listaAdapter = new PractitionersAdapter(this);

		dialogListMsg.show_listview().setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {
				dialogListMsg.Close();
				posIndex = pos;
				addMessage(true);
			}
		});

		dialogListMsg.submit_no().setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				dialogListMsg.Close();
			}
		});
		
		listPhoneMessage(false);

	}
	private void addMessage(boolean isShow) {
		AjaxParams params = new AjaxParams();
		params.put("action_flag", "updateApply");
		params.put("applyId", applyModel.getApplyId());
		params.put("applyUserName","患者姓名："+list_result.get(posIndex).getPersonName()+"-患者手机号码："+list_result.get(posIndex).getPersonPhone());
		params.put("applyMessageId", noticeModel.getDoctorId());
		params.put("applyMessageName", noticeModel.getDoctorName());
		httpPost(Consts.URL + Consts.APP.HospitalAction, params, Consts.actionId.resultFlag, isShow, "正在参与...");
	}

	private void listPhoneMessage(boolean isShow) {
		AjaxParams params = new AjaxParams();
		params.put("action_flag", "listMessagePerson");
		params.put("personUserId", MemberUserUtils.getUid(this) + "");
		httpPost(Consts.URL + Consts.APP.HospitalAction, params, Consts.actionId.resultCode, isShow, "正在加载...");
	}

	private List<PersonModel> list_result = new ArrayList<PersonModel>();

	@Override
	protected void callBackSuccess(ResponseEntry entry, int actionId) {
		super.callBackSuccess(entry, actionId);

		switch (actionId) {
		case Consts.actionId.resultCode:
			if (null != entry.getData() && !TextUtils.isEmpty(entry.getData())) {

				String jsonMsg = entry.getData().substring(1, entry.getData().length() - 1);
				if (null != jsonMsg && !TextUtils.isEmpty(jsonMsg)) {
					list_result.clear();
					list_result = mGson.fromJson(entry.getData(), new TypeToken<List<PersonModel>>() {
					}.getType());
					listaAdapter.setData(list_result);
					dialogListMsg.show_listview().setAdapter(listaAdapter);

				} else {
				}
			}
			break;
		case Consts.actionId.resultFlag:
			ShowObservable.getInstance().notifyStepChange("ok");
			ToastUtil.show(UpdatePayActivity.this, "预约成功");
			new Handler().postDelayed(new Runnable() {
				@Override
				public void run() {
					finish();
				}
			}, 2000);
			break;
		}

	}

}
